/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.impl;

import java.util.EnumMap;
import java.util.EnumSet;

import org.jbfilter.core.JbfilterComparatorKeyword;

/**
 * Wraps the {@link JbfilterComparatorKeyword}'s with additional features for bean filtering.
 * @author Marcus Adrian
 */
enum _JbfilterComparatorKeywordBean {
	eq(JbfilterComparatorKeyword.eq),
	lt(JbfilterComparatorKeyword.lt),
	gt(JbfilterComparatorKeyword.gt),
	le(JbfilterComparatorKeyword.le),
	ge(JbfilterComparatorKeyword.ge);

	private JbfilterComparatorKeyword wrapped;
	
	private static EnumMap<JbfilterComparatorKeyword, _JbfilterComparatorKeywordBean> wrapperRegistry
		= new EnumMap<JbfilterComparatorKeyword, _JbfilterComparatorKeywordBean>
			(JbfilterComparatorKeyword.class);
	
	private static final EnumSet<_JbfilterComparatorKeywordBean> equalitySet;
	private static final EnumSet<_JbfilterComparatorKeywordBean> greaterSet;
	private static final EnumSet<_JbfilterComparatorKeywordBean> lowerSet;
	
	static {
		for (_JbfilterComparatorKeywordBean wrapper : _JbfilterComparatorKeywordBean.values()) {
			wrapperRegistry.put(wrapper.wrapped, wrapper);
		}
		equalitySet = EnumSet.of(eq, le, ge);
		greaterSet =  EnumSet.of(gt, ge);
		lowerSet =  EnumSet.of(lt, le);
	}

	public static _JbfilterComparatorKeywordBean fromJbfilterComparatorKeyword(JbfilterComparatorKeyword wrapped) {
		return wrapperRegistry.get(wrapped);
	}
	// constructor
	private _JbfilterComparatorKeywordBean(JbfilterComparatorKeyword wrapped) {
		this.wrapped = wrapped;
	}
	
	public boolean pass(int compareValue) {
		if (compareValue == 0) {
			return (equalitySet.contains(this));
		} else if (compareValue > 0) {
			return (greaterSet.contains(this));
		} else {
			return (lowerSet.contains(this));
		}
	}
}
